//
//  CZMainTabBarController.swift
//  weibo10
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class CZMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //定义闭包
        
        let composeCallback = {
        () -> () in
            print("Modal出控制器")
        }
        
        //创建mainTabBar
        let mainTabBar = CZMainTabBar()
        
        
       //传递闭包
        mainTabBar.composeCallback = composeCallback 
        
        
        
        
        
        //get，只读属性，没有set方法，＝（等号）赋值调用的事set方法，只读属性没有了set就不能用 ＝（等号）直接赋值
        //使用KVC，KVC会先去找set方法，如果没有会找_tabBar成员变量，KVC运行时
        
        setValue(mainTabBar, forKey: "tabBar")
        
       setupChilderViewController()

 }
    
    
//        //创建UITabBarController的子控制器
//        let homeVC = UITableViewController()
//        
//        
//        //设置title
//        homeVC.title = "首页"
//        homeVC.tabBarItem.image = UIImage(named: "tabbar_home")
//        
//       .addChildViewController(UINavigationController(rootViewController: homeVC))
//        
//        
//        
//        let messageVC = UITableViewController()
//        messageVC.title = "消息"
//        messageVC.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        tabVC.addChildViewController(UINavigationController(rootViewController: messageVC))
//        
//        
//        
//        let discoverVC = UITableViewController()
//        discoverVC.title = "发现"
//        discoverVC.tabBarItem.image = UIImage(named: "tabbar_discover")
//        tabVC.addChildViewController(UINavigationController(rootViewController: discoverVC))
//        
//        
//        
//        let profileVC = UITableViewController()
//        profileVC.title = "我"
//        profileVC.tabBarItem.image = UIImage(named: "tabbar_profile")
//        
//        //tabVC添加子控制器
//        
//        tabVC.addChildViewController(UINavigationController(rootViewController: profileVC))
//        
//    
    //方法  创建子控制器
    func setupChilderViewController()
    {
        
       //首页
        let homeVC = CZHomeViewController()
        
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        //消息
        let messageVC = CZMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        
        //发现
        let discoverVC = CZDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        
        
        //我
        let profileVC = CZProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
        
    }
    
    
    
    //添加子控制器，设置标题和图片名称，包装导航控制器
    func addChildViewController(controller: UIViewController,title:String,imageName:String)
    {
       //设置标题
        controller.title = title
        
        //设置图片
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //设置高亮图片
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置文字选中的颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
        
        
        //包装导航控制器
        self.addChildViewController(UINavigationController(rootViewController: controller))
        
        
    }
    
    

}






